
angular.module('mainApp.orderManage', ['ui.router'])
    .config(function($stateProvider) {
        $stateProvider
            .state({//模板列表页面
                name : 'orderManage/index',
                url:'/orderManage/index',
                templateUrl:'views/orderManage/index.html?v='+new Date().getMinutes(),
                controller:'templateListCtrl'
            })
    })
    .controller('templateListCtrl',function($scope) {
        $scope.$emit('arg',2)
        $('.date-picker').datepicker({
            autoclose: true,
            language : 'zh-CN',
            format : 'yyyy-mm-dd'
        })
        $scope.refundModel={};
        var date=new Date();
        $scope.orderModel={
            "pageNo":1,
            "pageSize":10,
            "userName":"",
            "orderName":"",
            "cellPhone":"",
            "startDate":date.getFullYear() + "-" + (date.getMonth()) + "-" + date.getDate(),
            "endDate":date.getFullYear() + "-" + (date.getMonth()+1) + "-" + date.getDate()
        }
        $scope.pagination = function (currentPage, pageCount) {
            $('.page-function #pagination').remove();
            $('.page-function').html('<ul id="pagination" class="pagination"></ul>');
            $('.pagination').twbsPagination({
                totalPages: pageCount,
                startPage: currentPage,
                visiblePages: 5,
                first: "首页",
                prev: "上一页",
                next: "下一页",
                last: "尾页",
                href: 'javascript:;',
                //hideOnlyOnePage : true,
                onPageClick: function (event, page) {
                    $scope.getOrderList(page)
                }
            });
        };
        //订单列表
        $scope.getOrderList=function (page) {
            if(page){
                $scope.orderModel.pageNo=page;
                $scope.orderModel.pageSize=10;
            }
            if(!$scope.orderModel.startDate && $scope.orderModel.endDate){
                layer.msg('时间筛选不不符合规则！');
                return false;
            }
            if(parseInt(new Date($scope.orderModel.startDate).getTime()/1000) > parseInt(new Date($scope.orderModel.endDate).getTime()/1000)){
                layer.msg('开始时间不能大于结束时间！');
                return false;
            }
            layer.load(0, {shade: false}); //0代表加载的风格，支持0-2
            new HttpRequest().queryOrderList($scope.orderModel, function (res) {
                layer.closeAll();
                if (res.code == '000') {
                    $scope.orderlist = res.data.list || [];
                    $scope.$apply();
                    if ($scope.orderlist.length > 0) {
                        $scope.pagination($scope.orderModel.pageNo, res.data.pageCount);
                    }
                } else {
                    layer.msg('获取订单列表失败！')
                }
            })
        }
        $scope.getOrderList($scope.orderModel.pageNo)
        //尾款设置
        $scope.finalSet=function (orderId) {

            $scope.orderId=orderId;
        };
        $scope.commitSet=function () {
            if(!$scope.orderId){
                layer.msg('请完善信息');
                return false;
            }
            new HttpRequest().changeFinalPayment({finalPayment:$scope.finalPaymentSet,rowId:$scope.orderId},function (res) {
                if(res.code=='000'){
                    location.reload();
                }else{
                    layer.msg(res.msg);
                }
            })
        }
        //导出
        $scope.exportExecl=function () {
            delete $scope.orderModel.pageNo;
            delete $scope.orderModel.pageSize;
            if(parseInt(new Date($scope.orderModel.startDate).getTime()/1000) > parseInt(new Date($scope.orderModel.endDate).getTime()/1000)){
                layer.msg('开始时间不能大于结束时间！');
                return false;
            }
            if(!$scope.orderModel.startDate && $scope.orderModel.endDate){
                layer.msg('时间筛选不不符合规则！');
                return false;
            }
            new HttpRequest().orderExportExcel($scope.orderModel,function (res) {

                if(res.code=='000'){
                    location.href=baseUrl+res.data;
                }else{
                    layer.msg('导出失败！');
                }
            })
        };
        //支付尾款
        $scope.finalPayment=function (event) {
            var _this=$(event.target);
            var orderId=_this.attr('data-id');
            var name=_this.attr('data-name');
            var index = layer.confirm('是否确认要向'+name+'发送尾款支付信息？', {
                title: '尾款确认',
                btn: ['确定', '取消'],//按钮
            }, function () {
                new HttpRequest().finalPay({orderId:orderId},function (res) {
                    if(res.code='000'){
                        location.reload()
                    }else{
                        layer.msg(res.msg);
                    }
                })
            }, function () {
                layer.close(index)
            })
        };
        //通话记录
        $scope.conversationList=function (event) {
            var _this=$(event.target);
            var dataDetail=_this.closest('.callPhone').data('detail');
            var orderNo=dataDetail.orderNo;
            new HttpRequest().queryMobileLog({orderNo:orderNo},function (res) {
                if(res.code =='000'){
                    var conList=res.data || [];
                    var html='';
                    conList.map(function (index) {
                        html+= index.createTime+'，'+ (index.callStaus || '双向通话')+':'+index.callTime+'秒<br />';
                    });
                    if(html){
                        layer.open({
                            title:'通话记录',
                            type: 1,
                            area: ['420px', '240px'],
                            content: html
                        });
                    }else{
                        layer.msg('暂无通话记录')
                    }

                }else{
                    layer.msg('获取失败！')
                }
            })
        }

        //双向拨打
        $scope.callDouble=function () {
            var _this=$(event.target);
            var dataDetail=_this.closest('.callPhone').data('detail');
            var flag=true;
            //$scope.dataDetail= dataDetail;
            if(!dataDetail.doctors.length){
                layer.msg('该套餐下还没有关联医生！')
                return false
            }
            var str='请选择通话医生 : <select name="secDoc">';
            dataDetail.doctors.map(function (index) {
                str+='<option value="'+index.doctorMobile+'">'+index.dotorName+'</option>';
            });
            str+='</select><br />';
            str+='设置通话时间：';
            str+='<select name="chooseSec">' +
                '<option value="5">5</option>' +
                '<option value="10">10</option>' +
                '<option value="15">15</option>' +
                '<option value="20">20</option>' +
                '<option value="25">25</option>' +
                '<option value="30">30</option>' +
                '<option value="35">35</option>' +
                '<option value="40">40</option>' +
                '<option value="45">45</option>' +
                '<option value="50">50</option>' +
                '<option value="55">55</option>' +
                '<option value="60">60</option>' +
                '</select>'+'分钟';
            layer.confirm('接通',{
                type: 1,
                title:'接通',
                area: ['300px', '210px'], //宽高
                content: str,
                btn: ['确认拨打', '取消拨打']
            },function () {
                if(flag){
                    //new HttpRequest().doubleTalk({orderNo:dataDetail.orderNo ,wxOpenId:'suite',userMobile:dataDetail.patientPhone,doctorMobile:13666144049 || $('select[name="secDoc"]').val(),maxTime:$('select[name="chooseSec"]').val()},function (res) {
                   new HttpRequest().doubleTalk({orderNo:dataDetail.orderNo ,wxOpenId:'suite',userMobile:dataDetail.patientPhone,doctorMobile:$('select[name="secDoc"]').val(),maxTime:$('select[name="chooseSec"]').val()},function (res) {
                        if(res.code !='200' && res.data !='200'){
                            layer.msg('双向通话失败！');
                        }else{
                            layer.closeAll();
                            layer.msg('通话中。。。');
                        }
                    })
                    flag=false;
                }

            },function () {
                layer.closeAll();
            });
        }
    });


